package com.power.web.action.menu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.power.Constants;
import com.power.model.menu.TsMenu;
import com.power.model.user.TsSysuser;
import com.power.services.menu.MenuService;
import com.power.util.RequestUtil;
import com.power.web.action.base.AbstractBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class MenuAction extends AbstractBaseAction {

	@Autowired
	@Qualifier("menuService")
	private MenuService menuService;

	private String jsonString;
	private String menuname;
	private String url;
	private String title;
	private Integer menupid;
	private Integer menuid;
	private String permission1;
	private String permission2;
	private String permission3;
	private String permission4;
	private String permission5;
	private String permission6;
	private String permission7;
	private String permission8;

	public String bulidMenu4User() {
		return null;
	}

	public String buildMenuJSON() {
		List menuLst = this.menuService.findAll();
		try {
			JSONArray jsonObject = JSONArray.fromObject(menuLst);
			this.jsonString = jsonObject.toString();
		} catch (Exception e) {
			this.jsonString = "[]";
		}
		return Constants.JSONSTRING_RESULT_NAME;
	}
	
	public String loadMenu() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		String userGroup = user.getUserGroup();
		List menuLst = this.menuService.findAll(userGroup);
		try {
			JSONArray jsonObject = JSONArray.fromObject(menuLst);
			this.jsonString = jsonObject.toString();
		} catch (Exception e) {
			this.jsonString = "[]";
		}
		return Constants.JSONSTRING_RESULT_NAME;
	}

	public String deleteMenu() {
		int dropNodeID = RequestUtil.getParam(this.request, "deleteNodeID", -1);
		TsMenu menu = (TsMenu) this.menuService.read(new Integer(dropNodeID));
		this.menuService.delete(menu);
		try {
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print("{success:true,msg:'删除成功!'}");
		} catch (IOException e) {
			log.error("输出JSON数据错误！", e);
		}
		return null;
	}

	public String updateMenu() {
		TsMenu menu = (TsMenu) this.menuService.read(this.menuid);
		menu.setMenuname(this.menuname);
		menu.setUrl(this.url);
		menu.setTitle(this.title);
		menu
				.setPermission1((this.permission1 == null) ? "0"
						: this.permission1);
		menu
				.setPermission2((this.permission2 == null) ? "0"
						: this.permission2);
		menu
				.setPermission3((this.permission3 == null) ? "0"
						: this.permission3);
		menu
				.setPermission4((this.permission4 == null) ? "0"
						: this.permission4);
		menu
				.setPermission5((this.permission5 == null) ? "0"
						: this.permission5);
		menu
				.setPermission6((this.permission6 == null) ? "0"
						: this.permission6);
		this.menuService.update(menu);
		this.response.reset();
		try {
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(
					"{success:true,msg:'保存成功!',menuid:" + menu.getMenuid()
							+ "}");
		} catch (IOException e) {
			log.error("输出JSON数据错误！", e);
		}

		return null;
	}

	public String createMenu() {
		TsMenu menu = new TsMenu();
		menu.setMenuname(this.menuname);
		menu.setUrl(this.url);
		menu.setTitle(this.title);
		menu.setMenupid(this.menupid);
		menu
				.setPermission1((this.permission1 == null) ? "0"
						: this.permission1);
		menu
				.setPermission2((this.permission2 == null) ? "0"
						: this.permission2);
		menu
				.setPermission3((this.permission3 == null) ? "0"
						: this.permission3);
		menu
				.setPermission4((this.permission4 == null) ? "0"
						: this.permission4);
		menu
				.setPermission5((this.permission5 == null) ? "0"
						: this.permission5);
		menu
				.setPermission6((this.permission6 == null) ? "0"
						: this.permission6);
		this.menuService.create(menu);
		this.response.reset();
		try {
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(
					"{success:true,msg:'保存成功!',menuid:" + menu.getMenuid()
							+ "}");
		} catch (IOException e) {
			log.error("输出JSON数据错误！", e);
		}

		return null;
	}

	public String dropMenu() {
		int dropNodeID = RequestUtil.getParam(this.request, "dropNodeID", -1);
		int parentID = RequestUtil.getParam(this.request, "parentID", -1);
		int indexof = RequestUtil.getParam(this.request, "indexof", 0);
		this.menuService.dropMenu(dropNodeID, parentID, indexof + 1);
		try {
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print("{success:true,msg:'保存成功!'}");
		} catch (IOException e) {
			log.error("输出JSON数据错误！", e);
		}
		return null;
	}

	public String execute() {
		return null;
	}

	public String returnMethod(List list) {
		if (list == null)
			list = new ArrayList();
		try {
			JSONArray jsonObject = JSONArray.fromObject(list);
			this.jsonString = jsonObject.toString();
		} catch (Exception e) {
			this.jsonString = "[]";
		}
		return Constants.JSONSTRING_RESULT_NAME;
	}

	public String getPermission7() {
		return this.permission7;
	}

	public void setPermission7(String permission7) {
		this.permission7 = permission7;
	}

	public String getPermission8() {
		return this.permission8;
	}

	public void setPermission8(String permission8) {
		this.permission8 = permission8;
	}

	public Integer getMenuid() {
		return this.menuid;
	}

	public void setMenuid(Integer menuid) {
		this.menuid = menuid;
	}

	public Integer getMenupid() {
		return this.menupid;
	}

	public void setMenupid(Integer menupid) {
		this.menupid = menupid;
	}

	public String getMenuname() {
		return this.menuname;
	}

	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getJsonString() {
		return this.jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public String getPermission1() {
		return this.permission1;
	}

	public void setPermission1(String permission1) {
		this.permission1 = permission1;
	}

	public String getPermission2() {
		return this.permission2;
	}

	public void setPermission2(String permission2) {
		this.permission2 = permission2;
	}

	public String getPermission3() {
		return this.permission3;
	}

	public void setPermission3(String permission3) {
		this.permission3 = permission3;
	}

	public String getPermission4() {
		return this.permission4;
	}

	public void setPermission4(String permission4) {
		this.permission4 = permission4;
	}

	public String getPermission5() {
		return this.permission5;
	}

	public void setPermission5(String permission5) {
		this.permission5 = permission5;
	}

	public String getPermission6() {
		return this.permission6;
	}

	public void setPermission6(String permission6) {
		this.permission6 = permission6;
	}
}